//
//  NewMainHeadView.m
//  QoilPublic
//
//  Created by SunChao on 2018/1/12.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "NewMainHeadView.h"
#import "QActiveWebViewVC.h"
#import "QLoginVC.h"
#import "QWebViewVC.h"
@implementation NewMainHeadView

-(void)awakeFromNib{
    
    [super awakeFromNib];
   
//    [self createUI];
}
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
    }
    return self;
}


- (void)setImageWithData:(NSMutableArray *)array{
    self.imageData = array.mutableCopy;
    if (self.cycleScrollView) {
        [self.cycleScrollView removeFromSuperview];
        self.cycleScrollView = nil;
    }
    
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 230) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageControlAliment =  SDCycleScrollViewPageContolStyleAnimated;
    self.cycleScrollView.autoScrollTimeInterval = 2.5;
    self.cycleScrollView.currentPageDotColor = [UIColor grayColor]; // 自定义分页控件小圆标颜色
    NSMutableArray *imgurls = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < array.count; i++) {
        NSDictionary *tmpDic = [array objectAtIndex:i];
        NSString *imgurl = [tmpDic objectForKey:@"imgUrl"];
        [imgurls addObject:imgurl];
    }
    self.cycleScrollView.imageURLStringsGroup = imgurls;
    [self addSubview:self.cycleScrollView];
    
}
#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
     UIViewController *currentVC = [QViewUtility getCurrentVC];
    CLog(@"index===%ld",(long)index);
    if (_imageData.count == 0) {
        return;
    }
    NSDictionary *imgvoDic = _imageData[index];
    NSString *url = imgvoDic[@"url"];
    NSString *isVoucher = imgvoDic[@"isVoucher"];
    
    if ([isVoucher isEqualToString:@"1"]){
        if([QUserInfo sharedInfo].loginModel){
            if   (url.length > 0){
                QActiveWebViewVC *vc = [[QActiveWebViewVC alloc] init];
                vc.title = imgvoDic[@"title"];
                vc.url = url;
                [currentVC.navigationController pushViewController:vc animated:YES];
            }else{
                return;
            }
        }else{
            
            [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"您还未登录,请登录" leftTitle:@"取消" rightTitle:@"立即登录" type:1 myBlock:^(NSInteger tag) {
                if(tag == 1){
                    QLoginVC *vc = [[QLoginVC alloc] init];
                    [currentVC.navigationController pushViewController:vc animated:YES];
                }
            }];
        }
        
    }else{
        if(url.length > 0){
            QWebViewVC *vc = [[QWebViewVC alloc] init];
            vc.style = WebNetWorkStyle;
            vc.url = url;
            vc.title = imgvoDic[@"title"];
            [currentVC.navigationController pushViewController:vc animated:YES];
        }else{
            return;
        }
    }
}
// 滚动到第几张图回调
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
//    NSLog(@">>>>>> 滚动到第%ld张图", (long)index);
}
ARRAY_INIT(imageData)
@end

